package com.example.cyclops;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity {
	private final static String TAG = MainActivity.class.getSimpleName();

	MythDatabase mythdata = new MythDatabase(MainActivity.class.getPackage().getName() + "/files/maindata/");
	
	public static final int PLAY_ID = Menu.FIRST;
	public static final int EXIT_ID = Menu.FIRST + 1;
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			this.finish();
		}
		return false;
	}
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		// ==================设置全屏=========================
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_main);
		
		MythIPtable mythip = mythdata.getIndex();		
		Global.iptable = mythdata;
		Global.remoteip = mythip.getIP();
		Global.remoteport = mythip.getPort();
		Global.tmpstr = new ArrayList<String>();
		for(int i = 0;i < mythdata.size();i++){
			Global.tmpstr.add(mythdata.get(i).getKeyname());
		}
		final Spinner spinner = (Spinner)findViewById(R.id.spinner1);		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_single_choice,
				Global.tmpstr);
		spinner.setAdapter(adapter);
		if(Global.tmpstr.size() > Global.iptable.getListIndex())
			spinner.setSelection(Global.iptable.getListIndex());
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				Global.remoteip = Global.iptable.get(arg2).getIP();
				Global.remoteport = Global.iptable.get(arg2).getPort();
				EditText myname = (EditText)findViewById(R.id.username_edit);
				EditText mypass = (EditText)findViewById(R.id.password_edit);
				myname.setText(Global.iptable.get(arg2).getUsername());
				mypass.setText(Global.iptable.get(arg2).getPassword());
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		//ArrayList<MythIPtable> iptable = sqlite.ReadAllFromDataBase(sqlite.getReadableDatabase());

		final Button btn2 = (Button)findViewById(R.id.Button01);
		btn2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//final Spinner spinner = (Spinner)findViewById(R.id.spinner1);	
				//spinner.setSelection(0);

				EditText myname = (EditText)findViewById(R.id.username_edit);
				EditText mypass = (EditText)findViewById(R.id.password_edit);
				String strname = myname.getText().toString();
				String strpass = mypass.getText().toString();
				
				String ip = new DataUtils(Global.remoteip,Global.remoteport).SearchNanopi();
				if(!ip.equals("0.0.0.0")){
					DataUtils data = new DataUtils(Global.remoteip,Global.remoteport);
					int userid = data.DeepLogin(ip, strname, strpass);
					if(userid <= 0){
						Toast.makeText(
								MainActivity.this,
			                   "服务器连接失败，请重试", Toast.LENGTH_SHORT).show();
					}else{
						Intent intent = new Intent();
						Bundle bundle = new Bundle();
						bundle.putString("ip", ip);
						bundle.putInt("userid", userid);
						intent.putExtras(bundle);
						intent.setClass(MainActivity.this, MythDiscovery.class);
						startActivity(intent);
					}
				}else{
					Toast.makeText(
							MainActivity.this,
		                   "没有找到设备", Toast.LENGTH_SHORT).show();
				}
			}
		});
		final Button btn = (Button)findViewById(R.id.signin_button);
		btn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View source){
				Global.iptable.SetIndex(spinner.getSelectedItemPosition());
				Global.iptable.flush();
				Global.displaymode = 1;

				EditText myname = (EditText)findViewById(R.id.username_edit);
				EditText mypass = (EditText)findViewById(R.id.password_edit);
				String strname = myname.getText().toString();
				String strpass = mypass.getText().toString();
				DataUtils data = new DataUtils(Global.remoteip,Global.remoteport);
				int userid = data.Login(strname, strpass);
				if(userid > 0){
						Log.e("Login,My userid",userid + "");
						Intent intent = new Intent();
						Bundle bundle = new Bundle();
						bundle.putString("userid", userid + "");
						intent.putExtras(bundle);
						intent.setClass(MainActivity.this, LoadingActivity.class);
						startActivity(intent);
				}else{
					Toast.makeText(
							MainActivity.this,
		                   "服务器连接失败，请重试", Toast.LENGTH_SHORT).show();
					}
			}
		});
	}
}
